Skip to content

improve literal handling in pyi generator#5725

Merged
adhami3310 merged 1 commit intomainfrom
improve-literal-handling-in-pyi-generator
Aug 21, 2025
Merged

improve literal handling in pyi generator#5725
adhami3310 merged 1 commit intomainfrom
improve-literal-handling-in-pyi-generator

Conversation

@adhami3310
Copy link
Member

No description provided.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR enhances the pyi generator's type handling capabilities by addressing two specific gaps in literal and None value processing. The changes are made to the reflex/utils/pyi_generator.py file, which is responsible for generating .pyi stub files that provide type information for Python static analysis tools.

The first improvement expands None value detection in both the _get_type_hint() function (lines 148-149) and type_to_ast() function (lines 428-429). Previously, the code only checked for type(None) but missed direct None values, which could occur in type annotations. The new condition if value is type(None) or value is None: ensures both cases are properly handled and converted to the appropriate AST representation.

The second enhancement adds comprehensive support for typing.Literal types in the type_to_ast() function (lines 432-439). When the generator encounters a Literal type annotation (like Literal['red', 'green', 'blue']), it now creates a proper AST subscript node with the literal values as constants. This generates the correct stub representation: ast.Subscript with ast.Name(id="Literal") as the value and ast.Tuple containing ast.Constant nodes for each literal value.

These changes integrate seamlessly with the existing pyi generation pipeline. The PyiGenerator class uses these utility functions during its file scanning process to convert Python type annotations into their AST equivalents, which are then written to .pyi stub files. This ensures that modern Python typing constructs like Literal types are accurately preserved in the generated type stubs, maintaining compatibility with static analysis tools and IDEs.

Confidence score: 5/5

  • This PR is safe to merge with minimal risk as it only adds missing type handling without changing existing logic
  • Score reflects straightforward improvements to type conversion functions with clear, targeted fixes
  • No files require special attention as the changes are isolated improvements to utility functions

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 20, 2025

CodSpeed Performance Report

Merging #5725 will not alter performance

Comparing improve-literal-handling-in-pyi-generator (e9e2b31) with main (7acc7d9)

Summary

✅ 8 untouched benchmarks

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting that this doesn't change any current .pyi files... is this for a third party integration?

@adhami3310
Copy link
Member Author

it's for reflex enterprise, it occasionally fails :/

@adhami3310 adhami3310 merged commit bc99f35 into main Aug 21, 2025
40 of 41 checks passed
@adhami3310 adhami3310 deleted the improve-literal-handling-in-pyi-generator branch August 21, 2025 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants